package com.ling.pl.autoproject.definition.controller;

import com.ling.pl.autoproject.definition.model.ProgrammeEntity;
import com.ling.pl.autoproject.definition.model.ProgrammeVO;
import com.ling.pl.autoproject.definition.model.ProjectEntity;
import com.ling.pl.autoproject.definition.model.ProjectVO;
import com.ling.pl.autoproject.definition.programme.model.Programme;
import com.ling.pl.core.commons.exception.BusinessException;
import com.ling.pl.core.commons.support.DaoPage;
import com.ling.pl.core.commons.utils.AssertHelper;
import com.ling.pl.core.commons.utils.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.*;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;

/**
 * @author bo.wang
 */
@RequestMapping("/programme")
@Api(value = "programme", description = "API")
@RestController("programmeController")
public class ProgrammeController extends BaseProgrammeController {

    Log log = LogFactory.getLog(ProgrammeController.class);

    @ApiOperation(value = "列出所有")
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public List<ProgrammeVO> findProgrammeByList(@RequestParam Map params) throws Exception {
        log.info("findByList");
        return programmeService.findProgrammeVOByParams(params);
    }

    @ApiOperation(value = "分页列出")
    @RequestMapping(value = "/findByPage", method = RequestMethod.GET)
    public DaoPage findProgrammeByPage(@RequestParam Map params) throws Exception {
        log.info("findByPage");
        DaoPage daoPage = programmeService.findProgrammeVOByParams(params, PageUtils.getPageIndex(params), PageUtils.getPageSize(params));
        return daoPage;
    }

    @ApiOperation(value = "新建一个")
    @RequestMapping(value = "/", method = RequestMethod.POST)
    @Transactional
    public ProgrammeVO create(@ApiParam(value = "", required = true) @RequestBody ProgrammeVO programmeVO) {
        log.info("create");
        ProgrammeEntity entity = programmeService.convertProgrammeVOToEntity(programmeVO);
        programmeService.save(entity);
        return programmeService.convertProgrammeToVO(entity);
    }

    @ApiOperation(value = "获取单个")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @Transactional
    public ProgrammeVO findById(@ApiParam(value = "ID") @PathVariable(name = "id") String id) {
        log.info("findOne");
        AssertHelper.notEmpty_assert(id, "ID不能为空");
        ProgrammeEntity entity = (ProgrammeEntity) programmeService.get(ProgrammeEntity.class, id);
        if (entity != null) {
            return programmeService.convertProgrammeToVO(entity);
        } else {
            throw new BusinessException("没有找到指定ID的数据");
        }
    }

    @ApiOperation(value = "更新某个指定的实体")
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    @Transactional
    public ProgrammeVO update(@ApiParam(value = "ID") @PathVariable String id, @ApiParam(value = "更新的", required = true) @RequestBody ProgrammeVO programmeVO) {
        log.info("update");
        ProgrammeEntity entity = programmeService.convertProgrammeVOToEntity(programmeVO);
        programmeService.update(entity);
        return programmeService.convertProgrammeToVO(entity);
    }

    @ApiOperation(value = "更新指定主键数据（提供部分信息）")
    @RequestMapping(value = "/{ids_}", method = RequestMethod.PATCH)
    @Transactional
    public void updatePatch(@ApiParam(value = "ID") @PathVariable String ids_, @ApiParam(value = "更新的", required = true) @RequestBody Map programmeVO) {
        log.info("updatePatch");

    }

    @ApiOperation(value = "删除某个")
    @RequestMapping(value = "/{ids_}", method = RequestMethod.DELETE)
    @Transactional
    public void delete(@ApiParam(value = "删除的ID") @PathVariable String ids_) {
        log.info("delete");
        String[] ids = ids_.split(",");
        for (String id : ids) {
            AssertHelper.notEmpty_assert(id, "ID不能为空");
            Programme entity = (ProgrammeEntity) programmeService.get(ProgrammeEntity.class, id);
            programmeService.remove(entity);
        }
    }


    @ApiOperation(value = "列出所有功能")
    @RequestMapping(value = "/project/{programmeId}", method = RequestMethod.GET)
    public List<ProjectVO> findProjectByList(@ApiParam(value = "programmeId") @PathVariable String programmeId, @RequestParam Map params) throws Exception {
        log.info("findByList");
        return programmeService.findProjectVOByParams(params);
    }

    @ApiOperation(value = "分页列出功能")
    @RequestMapping(value = "/project/{programmeId}/findByPage", method = RequestMethod.GET)
    public DaoPage findProjectByPage(@ApiParam(value = "programmeId") @PathVariable String programmeId, @RequestParam Map params) throws Exception {
        log.info("findByPage");
        DaoPage daoPage = programmeService.findProjectVOByParams(params, PageUtils.getPageIndex(params), PageUtils.getPageSize(params));
        return daoPage;
    }

    @ApiOperation(value = "新建一个功能")
    @RequestMapping(value = "/project/{programmeId}", method = RequestMethod.POST)
    @Transactional
    public ProjectVO createProject(@ApiParam(value = "programmeId") @PathVariable String programmeId, @ApiParam(value = "", required = true) @RequestBody ProjectVO projectVO) {
        log.info("create");
        ProjectEntity entity = programmeService.convertProjectVOToEntity(projectVO);
        programmeService.save(entity);
        return programmeService.convertProjectToVO(entity);
    }

    @ApiOperation(value = "获取单个功能")
    @RequestMapping(value = "/project/{programmeId}/{id}", method = RequestMethod.GET)
    @Transactional
    public ProjectVO findProjectById(@ApiParam(value = "programmeId") @PathVariable String programmeId, @ApiParam(value = "ID") @PathVariable(name = "id") String id) {
        log.info("findOne");
        AssertHelper.notEmpty_assert(id, "ID不能为空");
        ProjectEntity entity = (ProjectEntity) programmeService.get(ProjectEntity.class, id);
        if (entity != null) {
            return programmeService.convertProjectToVO(entity);
        } else {
            throw new BusinessException("没有找到指定ID的数据");
        }
    }

    @ApiOperation(value = "更新指定主键数据的全部信息")
    @RequestMapping(value = "/project/{programmeId}/{id}", method = RequestMethod.PUT)
    @Transactional
    public ProjectVO updateProject(@ApiParam(value = "programmeId") @PathVariable String programmeId, @ApiParam(value = "ID") @PathVariable String id, @ApiParam(value = "更新的", required = true) @RequestBody ProjectVO projectVO) {
        log.info("update");
        ProjectEntity entity = programmeService.convertProjectVOToEntity(projectVO);
        programmeService.update(entity);
        return programmeService.convertProjectToVO(entity);
    }

    @ApiOperation(value = "更新指定主键数据（提供部分信息）")
    @RequestMapping(value = "/project/{programmeId}/{ids_}", method = RequestMethod.PATCH)
    @Transactional
    public void updateProjectPatch(@ApiParam(value = "programmeId") @PathVariable String programmeId, @ApiParam(value = "ID") @PathVariable String ids_, @ApiParam(value = "更新的", required = true) @RequestBody Map projectVO) {
        log.info("updatePatch");
    }

    @ApiOperation(value = "删除某个功能")
    @RequestMapping(value = "/project/{programmeId}/{ids_}", method = RequestMethod.DELETE)
    @Transactional
    public void deleteProject(@ApiParam(value = "programmeId") @PathVariable String programmeId, @ApiParam(value = "删除的ID") @PathVariable String ids_) {
        log.info("delete");
        String[] ids = ids_.split(",");
        for (String id : ids) {
            AssertHelper.notEmpty_assert(id, "ID不能为空");
            ProjectEntity entity = (ProjectEntity) programmeService.get(ProjectEntity.class, id);
            programmeService.remove(entity);
        }
    }

}

